<?php

/**
 * Description of placecategory
 *
 * @author libaoling
 * @time 2015-1-8
 */
class PlacecategoryApp extends BackendApp
{

    private $_category_mod;
    private $_max_layer = 4;

    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->PlacecategoryApp();
    }

    public function PlacecategoryApp()
    {
        parent::__construct();

        $this->_category_mod = & m('placecategory');
    }

    /* 管理 */

    public function index()
    {
        /* 取得商品分类 */
        $categories = $this->_category_mod->get_list(0);

        /* 先根排序 */
        foreach ($categories as $key => $val)
        {
            $categories[$key]['switchs'] = 0;
            if ($this->_category_mod->get_list($val['cate_id']))
            {
                $categories[$key]['switchs'] = 1;
            }
        }
        $this->assign('categories', $categories);

        $this->assign('max_layer', $this->_max_layer);

        /* 导入jQuery的表单验证插件 */
        $this->import_resource(array(
            'script' => 'jqtable.js,inline_edit.js',
            'style' => 'res:style/jqtreetable.css'
        ));
        $this->display('placecategory.index.html');
    }

    /* 异步去商品分类子元素 */

    public function ajax_cate()
    {
        if (!isset($_GET['id']) || empty($_GET['id']))
        {
            echo ecm_json_encode(false);
            return;
        }
        $cate = $this->_category_mod->get_list($_GET['id']);
        foreach ($cate as $key => $val)
        {
            $child = $this->_category_mod->get_list($val['cate_id']);
            $lay = $this->_category_mod->get_layer($val['cate_id']);
            if ($lay >= $this->_max_layer)
            {
                $cate[$key]['add_child'] = 0;
            }
            else
            {
                $cate[$key]['add_child'] = 1;
            }
            if (!$child || empty($child))
            {

                $cate[$key]['switchs'] = 0;
            }
            else
            {
                $cate[$key]['switchs'] = 1;
            }
        }
        header("Content-Type:text/html;charset=" . CHARSET);
        echo ecm_json_encode(array_values($cate));
        return;
    }

    /* 新增 */

    public function add()
    {
        if (!IS_POST)
        {
            /* 参数 */
            $pid = empty($_GET['pid']) ? 0 : intval($_GET['pid']);
            $category = array('parent_id' => $pid, 'sort_order' => 255, 'is_show' => 1);
            $this->assign('category', $category);

            $this->assign('parents', $this->_get_options());
            /* 导入jQuery的表单验证插件 */
            $this->import_resource(array(
                'script' => 'jquery.plugins/jquery.validate.js'
            ));
            $this->display('placecategory.form.html');
        }
        else
        {
            $data = array(
                'cate_name' => $_POST['cate_name'],
                'parent_id' => $_POST['parent_id'],
                'sort_order' => $_POST['sort_order'],
                'is_show' => $_POST['is_show'],
            );

            /* 检查名称是否已存在 */
            if (!$this->_category_mod->unique(trim($data['cate_name']), $data['parent_id']))
            {
                $this->show_warning('name_exist');
                return;
            }

            /* 检查级数 */
            $ancestor = $this->_category_mod->get_ancestor($data['parent_id']);
            if (count($ancestor) >= $this->_max_layer)
            {
                $this->show_warning('max_layer_error');
                return;
            }

            /* 保存 */
            $cate_id = $this->_category_mod->add($data);
            if (!$cate_id)
            {
                $this->show_warning($this->_category_mod->get_error());
                return;
            }
            //清除缓存
            $this->_category_mod->clear_cache();

            $this->admin_log('添加场景分类:' . $data['cate_name']);
            $this->show_message('添加成功', 'back_list', 'index.php?app=placecategory', '继续添加', 'index.php?app=placecategory&amp;act=add&amp;pid=' . $data['parent_id']
            );
        }
    }

    /* 检查分类名的唯一 */

    public function check_category()
    {
        $cate_name = empty($_GET['cate_name']) ? '' : trim($_GET['cate_name']);
        $parent_id = empty($_GET['parent_id']) ? 0 : intval($_GET['parent_id']);
        $cate_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
        if (!$cate_name)
        {
            echo ecm_json_encode(true);
            return;
        }
        if ($this->_category_mod->unique($cate_name, $parent_id, $cate_id))
        {
            echo ecm_json_encode(true);
        }
        else
        {
            echo ecm_json_encode(false);
        }
        return;
    }

    /* 编辑 */

    public function edit()
    {
        $id = empty($_GET['id']) ? 0 : intval($_GET['id']);
        if (!IS_POST)
        {
            /* 是否存在 */
            $category = $this->_category_mod->get_info($id);
            if (!$category)
            {
                $this->show_warning('gcategory_empty');
                return;
            }
            $this->assign('category', $category);
            /* 导入jQuery的表单验证插件 */
            $this->import_resource(array(
                'script' => 'jquery.plugins/jquery.validate.js'
            ));
            $this->assign('parents', $this->_get_options($id));
            $this->display('placecategory.form.html');
        }
        else
        {
            $data = array(
                'cate_name' => $_POST['cate_name'],
                'parent_id' => $_POST['parent_id'],
                'sort_order' => $_POST['sort_order'],
                'is_show' => $_POST['is_show'],
            );

            /* 检查名称是否已存在 */
            if (!$this->_category_mod->unique(trim($data['cate_name']), $data['parent_id'], $id))
            {
                $this->show_warning('name_exist');
                return;
            }

            /* 检查级数 */
            $depth = $this->_category_mod->get_depth($id);
            $ancestor = $this->_category_mod->get_ancestor($data['parent_id']);
            if ($depth + count($ancestor) > $this->_max_layer)
            {
                $this->show_warning('max_layer_error');
                return;
            }

            /* 保存 */
            $old_data = $this->_category_mod->get_info($id); // 保存前的数据
            $rows = $this->_category_mod->edit($id, $data);
            if ($this->_category_mod->has_error())
            {
                $this->show_warning($this->_category_mod->get_error());
                return;
            }
            $this->_category_mod->clear_cache(); //清除缓存

            $this->admin_log('修改场景分类:' . $data['cate_name']);
            $this->show_message('修改成功', 'back_list', 'index.php?app=placecategory', '继续修改', 'index.php?app=placecategory&amp;act=edit&amp;id=' . $id
            );
        }
    }

    //异步修改数据
    public function ajax_col()
    {
        $id = empty($_GET['id']) ? 0 : intval($_GET['id']);
        $column = empty($_GET['column']) ? '' : trim($_GET['column']);
        $value = isset($_GET['value']) ? trim($_GET['value']) : '';
        $data = array();
        if (in_array($column, array('cate_name', 'is_show', 'sort_order')))
        {
            $data[$column] = $value;
            $category = $this->_category_mod->get_info($id);
            if ($column == 'cate_name')
            {
                if (!$this->_category_mod->unique($value, $category['parent_id'], $id))
                {
                    echo ecm_json_encode(false);
                    return;
                }
            }
            $this->_category_mod->edit($id, $data);
            if (!$this->_category_mod->has_error())
            {
                $this->_category_mod->clear_cache(); //清除缓存
                echo ecm_json_encode(true);
            }
            $this->admin_log('异步修改场景分类:' . $data['cate_name']);
        }
        else
        {
            return;
        }
        return;
    }

    /* 删除 */

    public function drop()
    {
        $id = isset($_GET['id']) ? trim($_GET['id']) : '';
        if (!$id)
        {
            $this->show_warning('no_gcategory_to_drop');
            return;
        }

        $ids = explode(',', $id);

        $this->admin_log('删除场景分类:' . $id);

        if (!$this->_category_mod->drop($ids))
        {
            $this->show_warning($this->_category_mod->get_error());
            return;
        }

        $this->_category_mod->clear_cache(); //清除缓存

        $this->show_message('删除成功');
    }

    /* 构造并返回树 */

    private function &_tree($categories)
    {
        import('tree.lib');
        $tree = new Tree();
        $tree->setTree($categories, 'cate_id', 'parent_id', 'cate_name');
        return $tree;
    }

    /* 取得可以作为上级的商品分类数据 */

    private function _get_options($except = NULL)
    {
        $categories = $this->_category_mod->get_list();
        $tree = & $this->_tree($categories);

        return $tree->getOptions($this->_max_layer - 1, 0, $except);
    }

}
